<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item label="营地名称" prop="ydName">
        <el-input
          v-model="queryParams.ydName"
          placeholder="请输入营地名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="营业时间" prop="ydTime">
        <el-date-picker
          clearable
          v-model="queryParams.ydTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择营业时间"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item label="联系人" prop="ydContact">
        <el-input
          v-model="queryParams.ydContact"
          placeholder="请输入营地联系人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="营地类型" prop="ydType">
        <el-select
          v-model="queryParams.ydType"
          placeholder="请选择营地类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.camp_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="营地时间类型" prop="ydTimetype">
        <el-select
          v-model="queryParams.ydTimetype"
          placeholder="请选择营地时间类型"
          clearable
        >
          <el-option
            v-for="dict in dict.type.camp_status"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['camp:yd:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['camp:yd:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['camp:yd:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['camp:yd:export']"
          >导出</el-button
        >
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="ydList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="营地主键id" align="center" prop="ydId" />
      <el-table-column label="营地名称" align="center" prop="ydName" />
      <el-table-column label="营地位置经度" align="center" prop="ydJingdu" />
      <el-table-column label="营地具体地址" align="center" prop="ydLocation" />
      <el-table-column
        label="营业时间"
        align="center"
        prop="ydTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.ydTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>
      <el-table-column label="营地类型id" align="center" prop="ydTypeid" />
      <el-table-column label="营地联系人" align="center" prop="ydContact" />
      <el-table-column label="营地联系方式" align="center" prop="ydPhone" />
      <el-table-column
        label="营地图片"
        align="center"
        prop="ydImage"
        width="100"
      >
        <template slot-scope="scope">
          <image-preview :src="scope.row.ydImage" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createName" />
      <el-table-column label="更新人" align="center" prop="updateName" />
      <el-table-column label="营地位置纬度" align="center" prop="ydWeidu" />
      <el-table-column label="营地类型" align="center" prop="ydType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_type" :value="scope.row.ydType" />
        </template>
      </el-table-column>
      <el-table-column label="用户营地类型" align="center" prop="ydUsertype">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.camp_usertype"
            :value="scope.row.ydUsertype"
          />
        </template>
      </el-table-column>
      <el-table-column label="营地状态" align="center" prop="ydState">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.camp_status"
            :value="scope.row.ydState"
          />
        </template>
      </el-table-column>
      <el-table-column label="营地时间类型" align="center" prop="ydTimetype">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.camp_status"
            :value="scope.row.ydTimetype"
          />
        </template>
      </el-table-column>
      <el-table-column label="审核" align="center" prop="ydApprove">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.camp_approve"
            :value="scope.row.ydApprove"
          />
        </template>
      </el-table-column>
      <el-table-column label="置顶" align="center" prop="ydShow">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.camp_show" :value="scope.row.ydShow" />
        </template>
      </el-table-column>
      <el-table-column label="评价" align="center" prop="ydScore" />
      <el-table-column label="营地介绍" align="center" prop="ydJieshao" />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['camp:yd:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['camp:yd:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改营地对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="营地名称" prop="ydName">
          <el-input v-model="form.ydName" placeholder="请输入营地名称" />
        </el-form-item>
        <el-form-item label="营地位置经度" prop="ydJingdu">
          <el-input v-model="form.ydJingdu" placeholder="请输入营地位置经度" />
        </el-form-item>
        <el-form-item label="营地具体地址" prop="ydLocation">
          <el-input
            v-model="form.ydLocation"
            placeholder="请输入营地具体地址"
          />
        </el-form-item>
        <el-form-item label="营业时间" prop="ydTime">
          <el-date-picker
            clearable
            v-model="form.ydTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择营业时间"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item label="营地类型id" prop="ydTypeid">
          <el-input v-model="form.ydTypeid" placeholder="请输入营地类型id" />
        </el-form-item>
        <el-form-item label="营地联系人" prop="ydContact">
          <el-input v-model="form.ydContact" placeholder="请输入营地联系人" />
        </el-form-item>
        <el-form-item label="营地联系方式" prop="ydPhone">
          <el-input v-model="form.ydPhone" placeholder="请输入营地联系方式" />
        </el-form-item>
        <el-form-item label="营地图片" prop="ydImage">
          <image-upload v-model="form.ydImage" />
        </el-form-item>
        <el-form-item label="创建人" prop="createName">
          <el-input v-model="form.createName" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="更新人" prop="updateName">
          <el-input v-model="form.updateName" placeholder="请输入更新人" />
        </el-form-item>
        <el-form-item label="营地位置纬度" prop="ydWeidu">
          <el-input v-model="form.ydWeidu" placeholder="请输入营地位置纬度" />
        </el-form-item>
        <el-form-item label="营地类型" prop="ydType">
          <el-select v-model="form.ydType" placeholder="请选择营地类型">
            <el-option
              v-for="dict in dict.type.camp_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户营地类型" prop="ydUsertype">
          <el-select v-model="form.ydUsertype" placeholder="请选择用户营地类型">
            <el-option
              v-for="dict in dict.type.camp_usertype"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="营地状态" prop="ydState">
          <el-input v-model="form.ydState" placeholder="请输入营地状态" />
        </el-form-item>
        <el-form-item label="营地时间类型" prop="ydTimetype">
          <el-select v-model="form.ydTimetype" placeholder="请选择营地时间类型">
            <el-option
              v-for="dict in dict.type.camp_status"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="审核" prop="ydApprove">
          <el-input v-model="form.ydApprove" placeholder="请输入审核" />
        </el-form-item>
        <el-form-item label="置顶" prop="ydShow">
          <el-input v-model="form.ydShow" placeholder="请输入置顶" />
        </el-form-item>
        <el-form-item label="评价" prop="ydScore">
          <el-input v-model="form.ydScore" placeholder="请输入评价" />
        </el-form-item>
        <el-form-item label="营地介绍" prop="ydJieshao">
          <el-input v-model="form.ydJieshao" placeholder="请输入营地介绍" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listYd, getYd, delYd, addYd, updateYd } from "@/api/camp/yd";

export default {
  name: "Yd",
  dicts: ["camp_usertype", "camp_type", "camp_status"],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 营地表格数据
      ydList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ydName: null,
        ydJingdu: null,
        ydLocation: null,
        ydTime: null,
        ydTypeid: null,
        ydContact: null,
        ydPhone: null,
        ydImage: null,
        createName: null,
        updateName: null,
        ydWeidu: null,
        ydType: null,
        ydUsertype: null,
        ydState: null,
        ydTimetype: null,
        ydApprove: null,
        ydShow: null,
        ydScore: null,
        ydJieshao: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询营地列表 */
    getList() {
      console.log("-----------")
      this.loading = true;
      listYd(this.queryParams).then((response) => {
        this.ydList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ydId: null,
        ydName: null,
        ydJingdu: null,
        ydLocation: null,
        ydTime: null,
        ydTypeid: null,
        ydContact: null,
        ydPhone: null,
        ydImage: null,
        createName: null,
        createTime: null,
        updateName: null,
        updateTime: null,
        ydWeidu: null,
        ydType: null,
        ydUsertype: null,
        ydState: null,
        ydTimetype: null,
        ydApprove: null,
        ydShow: null,
        ydScore: null,
        ydJieshao: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.ydId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加营地";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ydId = row.ydId || this.ids;
      getYd(ydId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改营地";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.ydId != null) {
            updateYd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addYd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ydIds = row.ydId || this.ids;
      this.$modal
        .confirm('是否确认删除营地编号为"' + ydIds + '"的数据项？')
        .then(function () {
          return delYd(ydIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "camp/yd/export",
        {
          ...this.queryParams,
        },
        `yd_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
